﻿using System.Windows;
using System.Windows.Controls;

using SIMA.Client.Auxiliars.MVVM;
//using SIMA.Entities;
using SIMA.Core.Extension;
using System;
using SIMA.Controls;
using SIMA.Core.Enums;
using System.Collections.Generic;
using SIMA.Entities;

namespace SIMA.Client.Mantenimiento
{
    /// <summary>
    /// Lógica de interacción para frmAdminArea.xaml
    /// </summary>
    public partial class frmAdminArea : UserControl
    {
        #region Fields

        AdminAreaMvvm mvvmArea;

        #endregion

        #region Constructor

        public frmAdminArea()
        {
            Init();
            InitializeComponent();
            this.Resources.Add("AdminAreaMvvm", mvvmArea);
            LayoutRoot.DataContext = mvvmArea;
        }

        #endregion

        #region Methods

        private void Init()
        {
            mvvmArea = new AdminAreaMvvm();
            mvvmArea.CompleteAction += new EventHandler(mvvmArea_CompleteAction);
        }

        void mvvmArea_CompleteAction(object sender, EventArgs e)
        {
            busyIndicator.StopBusy();
        }

        private void btnGuardar_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                T_C_Estado estado = (T_C_Estado)cmbAreaEstados.SelectedItem;
                T_C_Area area = new T_C_Area();
                if (dgAreas.SelectedItem != null)
                {
                    busyIndicator.StartBusy("Actualizando Área.");
                    area = ((T_C_Area)dgAreas.SelectedItem).Clone();
                    area.Nombre = txtNombreArea.Text;
                    //area.IdEstado = estado.Id_Estado;
                    mvvmArea.Update((T_C_Area)dgAreas.SelectedItem, area);
                }
                else
                {
                    busyIndicator.StartBusy("Registrando nueva Área");
                    area.Nombre = txtNombreArea.Text;
                    //area.IdEstado = estado.Id_Estado;
                    mvvmArea.Register(area);
                }
            }
            catch (Exception ex)
            {
                busyIndicator.StopBusy();
                SIMAMessageBox.Show(DateTime.Now.ToLongTimeString(), ex.Message, ex.StackTrace, SIMAMessageBoxButtons.OK, SIMAMessageBoxImage.Error);

            }
        }

        private void btmCancelar_Click(object sender, RoutedEventArgs e)
        {
            txtNombreArea.Text = string.Empty;
            cmbAreaEstados.SelectedItem = null;
            dgAreas.ItemsSource = null;
        }

        private void btnEliminar_Click(object sender, RoutedEventArgs e)
        {
            if (dgAreas.SelectedItem != null)
            {
                busyIndicator.StartBusy("Eliminando información");
                T_C_Area area = ((T_C_Area)dgAreas.SelectedItem).Clone();
                mvvmArea.Delete(area);
            }
        }

        #endregion

        private void cmbFiltroPorEstado_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            T_C_Estado estado = (T_C_Estado)cmbFiltroPorEstado.SelectedItem;
            switch (estado.Nombre_Estado)
            {
                case "Activo":
                    mvvmArea.Filtro = FilterType.Active;
                    break;

                case "Inactivo":
                    mvvmArea.Filtro = FilterType.Inactive;
                    break;

                default:
                    break;
            }
        }

        private void dgAreas_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (dgAreas.SelectedItem != null)
            {
                T_C_Area area = (T_C_Area)dgAreas.SelectedItem;
                txtNombreArea.Text = area.Nombre;
                //cmbAreaEstados.SelectedItem = ((List<T_C_Estado>)cmbAreaEstados.ItemsSource).Find(c => c.Id_Estado == area.IdEstado);
            }
        }

    }
}
